|
|
|
|
|
|
|
No matter what development language and platform you use, at some point you will probably need to write code that interacts with underlying operating system files. For example, you might need to copy files or launch another program. You can do all this interactively by using Windows Explorer or an MS-DOS prompt window. As you will see in this chapter, you can accomplish these same file operations easily by using Visual Basic code. |
|
|
|
|
|
|
|
|
Another use for files is the storage and retrieval of information. In other chapters, you learned how to use databases to store information. However, sometimes the power of the Visual Basic database engine may not be necessary or even appropriate. For example, you may want to create a simple activity log or process a comma-delimited text file. In this chapter, you will examine a couple of options that allow you to store and access information in three types of text files: sequential files, random access files, and initialization (.INI) files. |
|
|
|
|
|
|
|
|
File Functions in Visual Basic |
|
|
|
|
|
|
|
|
Files are the basic organizational unit of an operating system. There are many different types of files, from user-created documents to games to operating system commands. This section discusses some of the functions used to work with files from Visual Basic. Another method of accessing files, using the FileSystemObject interface, is discussed in Chapter 30, "Using VBScript." (This object interface was originally a part of VBScript but is now available in Visual Basic starting with version 6.) The functions discussed in this section have been around for a long time and are considered to be the "traditional" Visual Basic file functions. |
|
|
|
|
|
|
|
|
See "Accessing the File System" p. 675 |
|
|
|
|
|
|
|
|
Using Dir to Find and List Files |
|
|
|
|
|
|
|
|
One useful file function is the Dir$ function. This function works like the Dir command at an MS-DOS command prompt. You can use the Dir$ function to retrieve a list of one or more operating system files that match a file specification or path. A path can include the name of a directory, a specific filename, or both. For example, C:\*.BAT is the path to all the files in the root directory of drive C having a BAT extension. The syntax of the Dir$ function is as follows: |
|
|
|
|
|
|
|
|
stringvar = Dir$(path[,attributes]) |
|
|
|
|
|
|
|
|
One use of Dir$ is to determine whether a file exists. If you try to open a database or access a file that does not exist, an error occurs. However, you can use Dir$ first to check for a file's existence before opening it, as in the following example: |
|
|
|
|
|
|
|
|
If Dir$("C:\MYFILE.TXT") = "" Then
Msgbox "The file was not found. Please try again!"
End If |
|
|
|
|
|
|
|
|
The Dir$ function returns the filename without the full path if the specified file is found, or it returns an empty string if no files were found. The preceding line of code displays a message box if MYFILE.TXT does not exist in the root directory of drive C. If the file does exist, the string myfile.txt is returned. To make things even simpler, you can create a generic function that returns a Boolean value True if the given file exists: |
|
|
|
|
|